using AutoMapper;
using BackData;
using Domain.DTOs.ProcessSteps;
using Domain.工艺工序;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Makesteps;

namespace Team.API.Applocation.Hanldler.Makesteps
{
    /// <summary>
    /// 成品工艺工序详情处理器
    /// </summary>
    public class GetMakestepDetailHandler : IRequestHandler<GetMakestepDetailCommand, APIResult<MakestepDetailDTO>>
    {
        private readonly IMapper mapper;
        private readonly IRepository<Makestepmodel> repository;

        public GetMakestepDetailHandler(IMapper mapper, IRepository<Makestepmodel> repository)
        {
            this.mapper = mapper;
            this.repository = repository;
        }

        public async Task<APIResult<MakestepDetailDTO>> Handle(GetMakestepDetailCommand request, CancellationToken cancellationToken)
        {
            // 仅允许查看顶级记录
            var entity = await repository.GetByIdAsync(request.Id);
            if (entity == null || entity.Parentid != 0 || entity.IsDel)
            {
                return APIResult<MakestepDetailDTO>.Faile("记录不存在或已删除");
            }

            var dto = mapper.Map<MakestepDetailDTO>(entity);
            return APIResult<MakestepDetailDTO>.Ok("查询成功", dto);
        }
    }
}

